﻿// // ******************************************************************
// //       /\ /|       @file       NfoParserHelper.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2024-12-27 09:12
// //    *(__\_\        @Copyright  Copyright (c) , 2024-12-27 09:12 Await
// // ******************************************************************

using System.Xml;
using System.Xml.Linq;

namespace AutomaticSubtitles.Service.Infrastructure.Helper;

public class NfoParserHelper
{
    /// <summary>
    /// 解析媒体的nfo文件信息
    /// </summary>
    /// <param name="content">NFO文件的内容字符串</param>
    /// <returns>解析后的Nfo对象</returns>
    public static (Nfo?, string? error) Parse(string content)
    {
        try
        {
            // 检查输入内容是否为空
            if (string.IsNullOrEmpty(content)) return default;

            // 创建XML文档对象
            var doc = XDocument.Parse(content);

            // 获取根元素
            var root = doc.Root;
            if (root == null) return default;

            // 创建新的Nfo对象
            var nfo = new Nfo
            {
                Title = root.Element("title")?.Value,
                OriginalTitle = root.Element("originaltitle")?.Value,
                Year = int.TryParse(root.Element("year")?.Value, out var year) ? year : 0,
                Rating = decimal.TryParse(root.Element("rating")?.Value, out var rating) ? rating : 0,
                Plot = root.Element("plot")?.Value,
                Outline = root.Element("outline")?.Value,
                TagLine = root.Element("tagline")?.Value,
                Runtime = root.Element("runtime")?.Value,
                Thumb = root.Element("thumb")?.Value,
                ReleaseDate = DateTime.TryParse(root.Element("releasedate")?.Value, out var releaseDate) ? releaseDate : null,
                // 添加更多常用媒体信息
                MPAA = root.Element("mpaa")?.Value,
                Studio = root.Element("studio")?.Value,
                Trailer = root.Element("trailer")?.Value,
                Language = root.Element("language")?.Value,
                Country = root.Element("country")?.Value,
                Premiered = root.Element("premiered")?.Value,
                FileInfo = root.Element("fileinfo")?.Value,

                // 添加评分计数
                VoteCount = int.TryParse(root.Element("votes")?.Value, out var votes) ? votes : 0,

                // 添加集合信息（适用于系列电影）
                Set = root.Element("set")?.Value,

                // 处理演员列表
                Actors = root.Elements("actor")?.Select(a => new Actor
                {
                    Name = a.Element("name")?.Value,
                    Role = a.Element("role")?.Value,
                    Thumb = a.Element("thumb")?.Value
                }).ToList(),

                // 处理类型列表
                Genres = root.Elements("genre")?.Select(g => g.Value).ToList(),

                // 处理导演列表
                Directors = root.Elements("director")?.Select(d => d.Value).ToList()
            };

            return (nfo, null);
        }
        catch (XmlException ex)
        {
            return (null, $"XML解析错误: {ex.Message}");
        }
        catch (Exception ex)
        {
            return (null, $"解析过程出现错误: {ex.Message}");
        }
    }
}